package ltd.dujiabao.dex.core.emitter;

import java.lang.reflect.Proxy;

import org.springframework.beans.factory.FactoryBean;

import lombok.Data;
import ltd.dujiabao.dex.core.DelayJobEntity;

/**
 * @author : DuJiabao
 * @Project : common-delay-executor
 * @Time : 2024/3/26 21:22
 * @Desc :
 */
@Data
public class EmitterFactoryBean<T> implements FactoryBean<T> {
    protected Class<T> type;
    private DelayJobEntity delayJob;
    private EmitterFactoryHolder emitterFactoryHolder;

    @Override
    public T getObject() throws Exception {
        EmitterFactory emitterFactory = emitterFactoryHolder.factory(delayJob.getExecutorType());
        return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{type}, emitterFactory.getEmitter(delayJob));
    }

    @Override
    public Class<?> getObjectType() {
        return type;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
